package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.DateFormatterIST;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import lombok.Data;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.Formatter;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-10-13 17:13
 **/
@TargetUrl(value = "https://techdatanewsflash.co.uk/news/*?/[0-9]{4}")
@Data
public class Techdata implements AfterExtractor {
	@ExtractBy(value = "//h1/text()", notNull = true)
	private String title;

	@ExtractBy(value = "//div[@id='article']//p/text()", notNull = true)
	private List<String> content;

	@ExtractBy("//div[@class='block-image']//img/@src")
	private List<String> imgList;

	@Override
	public void afterProcess(Page page) {
		ISpiderNewsService service = SpringUtils.getBean(ISpiderNewsService.class);
		SpiderNews spider = new SpiderNews();
		spider.setNewsTitle(ChinaNews.filterSpecialCharacter(title));
		StringBuffer sb = new StringBuffer();
		content.forEach(s -> sb.append(s + "$$$"));
		spider.setNewsContent(ChinaNews.filterSpecialCharacter(sb.toString()));
		spider.setContentSize(spider.getNewsContent().length());
		List<String> newImg = delPic(imgList);
//		spider.setNewsPicJson(JSONUtil.toJsonStr(newImg));
		spider.setNewsUrl(page.getUrl().toString());
		Date date = new Date();
		spider.setNewsPublicTime(date);
		spider.setUpdateBy("admin");
		spider.setCreateTime(DateUtil.date());
		spider.setNewsCategory("Techdata-IndiaTech");
		spider.setNewsSource("https://techdatanewsflash.co.uk/news");

		if (sb.length() < 50000 && sb.toString().replaceAll("\\$|[\\s\\p{Zs}]", "").length() > 20 && imgList.size() < 20) {
			RedisService redisService = SpringUtils.getBean(RedisService.class);
			Date ago = DateUtil.offsetDay(new Date(), -3);
			int i = date.compareTo(ago);
			//redis查询是否存在
			boolean ismember = redisService.isMember(SecureUtil.md5(title));
			if (!ismember && i > 0) {//如果不存在插入mysql和redis
				List<String> resImg = DataSaveUtil.delPic(newImg);
				spider.setNewsPicJson(JSONUtil.toJsonStr(resImg));
				service.save(spider);
				redisService.addTitleSet(SecureUtil.md5(title), spider.getNewsCategory(), date, title);
			}
		}

	}

	public List<String> delPic(List<String> imgList) {
		List<String> newImg = new ArrayList<>();
		for(String img:imgList){
			newImg.add("https://techdatanewsflash.co.uk/"+img);
		}
		return newImg;
	}

}
